package xian.woniu.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xian.woniu.entity.Diagnosis;
import xian.woniu.entity.Patient;
import xian.woniu.mapper.customer.CusDiagnosisMapper;
import xian.woniu.service.IDiagnosisService;
import xian.woniu.service.IDoctorService;
import xian.woniu.service.IOrderService;
import xian.woniu.service.IPatientService;
import xian.woniu.util.ResultVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author neide
 * @time 2022/07/01 15:26:40
 */
@Service
@Transactional
public class DiagnosisServiceImpl implements IDiagnosisService {

    private CusDiagnosisMapper diagnosisMapper;

    private IOrderService orderService;

    private IPatientService patientService;

    private IDoctorService doctorService;

    public DiagnosisServiceImpl(CusDiagnosisMapper diagnosisMapper, IOrderService orderService, IPatientService patientService, IDoctorService doctorService) {
        this.diagnosisMapper = diagnosisMapper;
        this.orderService = orderService;
        this.patientService = patientService;
        this.doctorService = doctorService;
    }

    @Override
    public List<Diagnosis> findByPid(Integer pid) {
        List<Diagnosis> diagnosisList = diagnosisMapper.selectByPid(pid);
        return diagnosisList;
    }

    @Override
    public List<Diagnosis> findByDid(Integer did) {
        List<Diagnosis> diagnosisList = diagnosisMapper.selectByDid(did);
        return diagnosisList;
    }

    @Override
    public List<Diagnosis> findAllPatient() {
        List<Diagnosis> diagnosisList = diagnosisMapper.selectAll();
        return diagnosisList;
    }

    @Override
    public void addDiagnosis(Diagnosis diagnosis) {
        diagnosisMapper.insert(diagnosis);
    }

    @Override
    public void modDiagnosis(Diagnosis diagnosis) {
        diagnosisMapper.updateByPrimaryKeySelective(diagnosis);
    }

    @GlobalTransactional
    @Override
    public List findOrderBypid(String pid) {

        List orders = new ArrayList();
        Patient object = patientService.selPatinfo(Integer.parseInt(pid));
        ResultVO resultVO1 = orderService.findOrderByPid(pid);
        List<Map> orderList = (List<Map>) resultVO1.getData();
        for (int i = 0; i < orderList.size(); i++) {
            Map<String, Object> patient = JSON.parseObject(JSON.toJSONString(object),new TypeReference<Map<String, Object>>(){});
            Integer did = (Integer) orderList.get(i).get("did");
            ResultVO resultVO2 = doctorService.findOne(String.valueOf(did));
            Map doctor= (Map) resultVO2.getData();
            patient.putAll(orderList.get(i));
            patient.putAll(doctor);
            orders.add(patient);
        }

        return orders;
    }

    @Override
    public List findDiagnosisByPid(String pid) {
        List orders = new ArrayList();
        Patient object = patientService.selPatinfo(Integer.parseInt(pid));
        List<Diagnosis> diagnosisList = diagnosisMapper.selectByPid(Integer.parseInt(pid));
        for (int i = 0; i < diagnosisList.size(); i++) {
            Map<String, Object> patient = JSON.parseObject(JSON.toJSONString(object),new TypeReference<Map<String, Object>>(){});
            Map<String, Object> diagnosis = JSON.parseObject(JSON.toJSONString(diagnosisList.get(i)),new TypeReference<Map<String, Object>>(){});
            patient.putAll(diagnosis);
            orders.add(patient);
        }
        return orders;
    }
    @Override
    public void save(Diagnosis diagnosis) {
        diagnosisMapper.insertSelective(diagnosis);
    }
}
